package com.qiqidream.admin.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiqidream.admin.common.utils.tree.TreeUtil;
import com.qiqidream.admin.system.mapper.MenuMapper;
import com.qiqidream.admin.system.model.dto.MenuDto;
import com.qiqidream.admin.system.model.entity.Menu;
import com.qiqidream.admin.system.model.vo.MenuVo;
import com.qiqidream.admin.system.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author QiQiDream
 * @since 2019/11/18 10:53
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<MenuVo> getMenus() {
        List<Menu> menuList = list(new QueryWrapper<Menu>().orderByAsc("type","sort"));
        List<MenuVo> convert = Convert.convert(new TypeReference<List<MenuVo>>() {}, menuList);
        return TreeUtil.getTreeList(0L, convert);
    }

    @Override
    public Boolean addMenu(MenuDto menuDto) {
        Menu menu = menuDto.getMenu();
        return save(menu);
    }

    @Override
    public Boolean editMenu(MenuDto menuDto) {
        Menu menu = menuDto.getMenu();
        menu.setId(menuDto.getId());
        return updateById(menu);
    }

    @Override
    public Boolean removeMenu(Long id) {
        List<Long> ids = baseMapper.getChildrenId(id);
        ids.add(id);
        return removeByIds(ids);
    }

    @Override
    public List<Long> getCodeByUrl(String url,String method) {
        return baseMapper.getCodeByUrl(url,method);
    }
}
